package dao.impl;

import com.google.common.collect.ImmutableMap;
import dao.OfferDao;
import domain.Offer;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by aceyin on 15/6/29.
 */
@Repository
public class OfferDaoImpl extends BaseGenericDao<Offer, Long> implements OfferDao {
    @Override
    public List<Offer> queryByRecruitmentId(Long id) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder().put("recruitment_id", id).build();
        return queryList("query-work-apply-by-recruitment-id", params);
    }

    @Override
    public Offer queryByApplierAndRecruitmentId(long applier, long recruitmentId) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("recruitment_id", recruitmentId)
                .put("applier", applier)
                .build();
        return queryOne("query-by-applier-and-recruitment-id", params);
    }

    @Override
    public List<Offer> findRecruitmentOffersByStatus(long recruitment, String status) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("recruitment", recruitment)
                .put("status", status)
                .build();
        return queryList("find-recruitment-offer-by-status", params);
    }

    @Override
    public void updateOfferStatus(String status, List<Long> ids) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("status", status)
                .put("ids", ids)
                .build();
        update("update-offer-status", params);
    }

    @Override
    public Offer findOffer(long recruitmentId, long applier) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("recruitment_id", recruitmentId)
                .put("applier", applier)
                .build();
        return queryOne("find-offer-for-user", params);
    }

    @Override
    public int countOfferByApplierAndEmployer(long applier, long employer) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("applier", applier)
                .put("employer", employer)
                .build();
        return count("count-offer-by-applier-and-employer", params);
    }

    @Override
    public List<Long> getApplierIds(long id) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder().put("id", id).build();
        return queryList("find-applier-ids-by-offer-id", params);
    }

    @Override
    public List<Offer> findUserOffersByStatus(long user, String status, int page, int rows) {
        int offset = getOffset(page, rows);
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("user", user)
                .put("status", status)
                .put("offset", offset)
                .put("limit", rows)
                .build();
        return queryList("find-user-offer-by-status", params);
    }

    @Override
    public List<Map<String, Object>> findWorkmates(Long recruitmentId) {
        Map<String, Object> params = newMapBuilder().put("recruitment", recruitmentId).build();
        return queryList("find-workmates", params);
    }

    @Override
    public void updateRateResult(Long id, String rate) {
        Map<String, Object> params = newMapBuilder().put("id", id).put("rate_result", rate).build();
        update("update-rate-result-by-id", params);
    }

    @Override
    public List<Offer> getAllOffersByStatus(long recruitmentId, long employerId, String status) {
        Map<String, Object> params = newMapBuilder()
                .put("recruitment_id", recruitmentId)
                .put("employer", employerId)
                .put("status", status)
                .build();
        return queryList("get-offers-by-recruitment-and-employer-and-status", params);
    }

    @Override
    public void updatePayResult(List<Long> offerIds, String status) {
        Map<String, Object> params = newMapBuilder()
                .put("ids", offerIds)
                .put("payResult", status)
                .build();
        update("batch-update-offer-pay-status", params);
    }
}
